package fm.dongman.animefm.view.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.android.flexbox.FlexboxLayout;

import fm.dongman.animefm.R;
import fm.dongman.animefm.view.util.ToastHelper;

/**
 * @description 动漫详情 观看资源反馈dialog
 * Created by xuyougen on 2017/11/27.
 */

public class AnimeWatchSourceFeedbackDialog extends Dialog {
    private AnimeWatchSourceFeedbackDialog(@NonNull Context context) {
        this(context, 0);
    }

    private AnimeWatchSourceFeedbackDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    public static class Builder {
        private Context mContext;
        private String mTitle;
        private OnFeedBackListener mListener;
        private String[] mTypes;
        private int mCurrentType = -1;
        private int[] mTypeId;
        private boolean mIsMustHaveContent = false;

        public Builder(Context context) {
            mContext = context;
        }

        public Builder setTypesAndId(String[] types, int[] id) {
            mTypes = types;
            mTypeId = id;
            return this;
        }

        public Builder setOnFeedbackListener(OnFeedBackListener listener) {
            mListener = listener;
            return this;
        }

        public Builder setTitle(String title) {
            mTitle = title;
            return this;
        }

        public Builder setIsMustHaveContent(boolean b) {
            mIsMustHaveContent = b;
            return this;
        }

        public AnimeWatchSourceFeedbackDialog build() {
            final AnimeWatchSourceFeedbackDialog dialog = new AnimeWatchSourceFeedbackDialog(mContext, R.style.CustomDialog);

            View layoutView = View.inflate(mContext, R.layout.dialog_anime_watch_source_feedback, null);
            TextView titleView = (TextView) layoutView.findViewById(R.id.mTvAnimeFeedbackTitle);
            final EditText content = (EditText) layoutView.findViewById(R.id.mEtAnimeFeedbackContent);
            Button btnConfirm = (Button) layoutView.findViewById(R.id.mBtnAnimeFeedbackConfirm);
            Button btnCancel = (Button) layoutView.findViewById(R.id.mBtnAnimeFeedbackCacnel);

            if (mTitle != null)
                titleView.setText(mTitle);

            //放置反馈类型的布局
            final FlexboxLayout subject = (FlexboxLayout) layoutView.findViewById(R.id.mfbAnimeFeedbackSubject);
            //将反馈类型遍历放进布局
            for (int i = 0; i < mTypes.length; i++) {
                TextView textView = (TextView) View.inflate(mContext, R.layout.item_anime_details_watch_source_btn, null);
                textView.setText(mTypes[i]);
                textView.setTag(mTypeId[i]);
                final int position = i;
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        changeSelectedStateAndStyle(subject, position);
                    }
                });
                subject.addView(textView);
            }
            //默认选中第一个类型
            changeSelectedStateAndStyle(subject, 0);

            //确认按钮事件
            btnConfirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mCurrentType == -1) {
                        ToastHelper.getInstance(mContext).showError("你没选择类型");
                        return;
                    }
                    if (mIsMustHaveContent && content.getText().toString().trim().length() == 0) {
                        ToastHelper.getInstance(mContext).showWarn("必须要填写内容哦");
                        return;
                    }
                    if (mListener != null)
                        mListener.onCommit(dialog, mCurrentType, content.getText().toString());
                }
            });

            //取消按钮事件
            btnCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null)
                        mListener.onCancel(dialog);
                }
            });
            dialog.setContentView(layoutView);
            return dialog;
        }

        /**
         * 类型选中及改变样式
         *
         * @param layout
         * @param position
         */
        private void changeSelectedStateAndStyle(FlexboxLayout layout, int position) {
            //设置当前类型
            mCurrentType = mTypeId[position];
            //清除所有按钮样式
            for (int i = 0; i < layout.getChildCount(); i++) {
                layout.getChildAt(i).setBackgroundColor(Color.parseColor("#ffffffff"));
                ((TextView) layout.getChildAt(i)).setTextColor(ContextCompat.getColor(mContext, R.color.home_sub_title_color));
            }
            //为当前选中的按钮设置选中样式
            layout.getChildAt(position).setBackgroundResource(R.drawable.bg_feedback_type_selected);
            ((TextView) layout.getChildAt(position)).setTextColor(ContextCompat.getColor(mContext, R.color.accent));
        }

        //回调接口
        public interface OnFeedBackListener {
            void onCommit(Dialog dialog, int currentType, String content);

            void onCancel(Dialog dialog);
        }
    }
}
